<?php

include_once("user.class.php");
include_once("operacionesBD.php");

class UserMapper
{

    public static function findByUserid($userid) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "SELECT * from users WHERE userid = '$userid'";
        $data = $dhb->consultar($query);
        if (!$data) return false;
        return new User($userid, $data[0]['username'], $data[0]['password'], $data[0]['mail']);
    }

    public static function findByUsername($username) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "SELECT * from users WHERE username = '$username'";
        $data = $dbh->consultar($query);
        if (!$data) return false;
        return new User($data[0]['userid'], $username, $data[0]['password'], $data[0]['mail']);
    }

    public function update(User $user) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "UPDATE users SET username = '{$user->get_username()}', password = '{$user->get_password()}', mail = '{$user->get_mail()}' WHERE userid = '{$user->get_userid()}'";
        $data = $dbh->modificar($query);
    }

    public function insert(User $user) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "INSERT INTO users VALUES ('{$user->get_userid()}', '{$user->get_username()}', '{$user->get_password()}', '{$user->get_mail()}')";
        $id = $dbh->modificar($query);
        return $id;
    }

    public function delete(User $user) {
        $dbh = operacionesBD::getInstance('localhost', 'root', '', 'musica');
        $query = "DELETE FROM users WHERE userid = '{$user->get_userid()}'";
        $data = $dbh->modificar($query);
    }

}

?>
